<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/9/1
 * Time: 16:42
 */
namespace app\shop\controller;
use think\Db;
class Duizhang extends Base
{

    public function index()
    {
        extract(input());
        $page = input('param.page');
        $type = input('param.type')?input('param.type'):1;//1全部  2已结算  3待结算  4已逾期
        $user_id = session('user_id');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }

        $data = array();
        if($type==1){
            $where['a.order_qy_user'] = $user_id;
            $where['a.order_status'] = array('>',7);
//            $where['a.con_type'] = 1;
            $data = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->order('a.order_fb_time desc')
                ->where($where)
                ->field('a.*,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
                ->paginate(50);
        }elseif ($type==2){
            $where['a.order_qy_user'] = $user_id;
            $where['a.order_status'] = array('>',7);
//            $where['a.con_type'] = 1;
            $where['a.jiesuan'] = 2;
            $data = Db::table('up_order')->alias('a')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->order('a.order_fb_time desc')
                ->where($where)
                ->field('a.*,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
                ->paginate(50);
        }elseif ($type==3){
            $where['a.order_qy_user'] = $user_id;
            $where['a.order_status'] = array('>',7);
            $where['a.con_type'] = 1;
            $where['a.jiesuan'] = 1;
            $data = Db::table('up_order')->alias('a')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->order('a.order_fb_time desc')
                ->where($where)
                ->field('a.*,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
                ->paginate(50);
        }elseif ($type==4){
            $where['a.order_qy_user'] = $user_id;
            $where['a.order_status'] = array('>',7);
            $where['a.con_type'] = 1;
            $where['a.jiesuan'] = 1;
            $where['a.order_contract_date'] = array('<',date('Y-m-d'));
            $data = Db::table('up_order')->alias('a')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->order('a.order_fb_time desc')
                ->where($where)
                ->field('a.*,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
                ->paginate(50);
        }
        $map['order_qy_user'] = $user_id;
        $map['order_status'] = array('>',7);
        $map['con_type'] = 1;
        $map['order_contract_date'] = array('<',date('Y-m-d'));
        $order_freight = Db::table('up_order')->where($map)->sum('order_freight');
        return $this->fetch ('order/duizhang',['data'=>$data,'pages'=>$pages,'type'=>$type,'order_freight'=>$order_freight]);
    }
    //确认账单
    public function bill(){
        $user_id = session('user_id');
//        $user_id = 5;
        if(request()->isPost()){
            $ids = input('ids');//以逗号分隔
            $ids = trim($ids,',');//以逗号分隔
            if(empty($ids)){
            return json(['code' => 100, 'msg' => '请选择需要确认账单的数据']);
        }
         $dataAll = explode(',',$ids);
         $a = 1;
         foreach ($dataAll as $key=>$vo){
                $res = Db::table('up_order')->where('order_id',$vo)->field('order_true_date')->find();
                if($res['order_true_date'] != ''||$res['order_true_date']!=null){
                    $a = 2;
                }
            }
         if($a==2){
             return json(['code' => 100, 'msg' => '已经有确认过账单,不能重复选择']);
         }else{
             //查询订单总的运费金额
             $order_freight = Db::table('up_order')->where('order_id','in',$ids)->sum('order_freight');
            //查询这个企业的加盟商ID
             $f_user_id = Db::table('up_u_user')->where('user_id',$user_id)->value('f_user_id');
             $where['union_jms_user_id'] = $f_user_id;
             $where['union_qy_user_id'] = $user_id;
             $result = Db::table('up_invoice_union')->where($where)->find();
             $data['user_id'] = $f_user_id;
             $data['qy_user_id'] = $user_id;
             $data['invoice_company'] = $result['union_company'];
             $data['invoice_address'] = $result['union_address'];
             $data['invoice_phone'] = $result['union_phone'];
             $data['invoice_bank'] = $result['union_bank'];
             $data['invoice_account'] = $result['union_account'];
             $data['invoice_num'] = $result['union_num'];
             $data['invoice_price'] = $order_freight;
//             $data['invoice_time'] = date('Y-m-d H:i:s');
             $data['invoice_order'] = $ids;
             $list = Db::table('up_invoice')->insertGetId($data);
             if($list){
                 $result1['order_true_date'] = date('Y-m-d H:i:s');
                 $result1['jiesuan'] = 2;
                  Db::table('up_order')->where('order_id','in',$ids)->update($result1);
                 return json(['code' => 200, 'msg' => '确认过账单成功']);
             }else{
                 return json(['code' => 100, 'msg' => '确认过账单失败']);
             }
         }
       }
    }

    public function bill1(){
//        $user_id = session('user_id');
//        $user_id = 5;
        if(request()->isPost()){
            $ids = input('ids');//以逗号分隔
            $image = input('image');//以逗号分隔
            $ids = trim($ids,',');//以逗号分隔
            if(empty($ids)){
                return json(['code' => 100, 'msg' => '请选择需要上传凭证的数据']);
            }
            if(empty($image)){
                return json(['code' => 100, 'msg' => '请上传凭证']);
            }
            $dataAll = explode(',',$ids);
            $a = 1;
            foreach ($dataAll as $key=>$vo){
                $res = Db::table('up_order')->where('order_id',$vo)->field('order_scpz_img')->find();
                if($res['order_scpz_img'] != ''||$res['order_scpz_img']!=null){
                    $a = 2;
                }
            }
            if($a==2){
                return json(['code' => 100, 'msg' => '已经有上传过账单凭证,不能重复选择']);
            }else{
                $list = Db::table('up_order')->where('order_id','in',$ids)->setField('order_scpz_img',$image);

                if($list){
                    return json(['code' => 200, 'msg' => '上传凭证成功']);
                }else{
                    return json(['code' => 100, 'msg' => '上传凭证失败']);
                }
            }
        }
    }
}